Imports BikiSoft.Comm.Comm_Frame
Friend Class Comm_Exceptions
    Inherits System.Exception

    Private m_ErrNum As BikiFrameResponse
    Private m_ErrType As BikiFrameType
    Private m_ErrDesc As String = ""
    Private m_Throw As Boolean = False
    Private m_By As BikiFrameID = BikiFrameID.ID_UNKNOWN

    Sub New(ByVal ErrNum As BikiFrameResponse, Optional ByVal By As BikiFrameID = BikiFrameID.ID_UNKNOWN)
        m_ErrNum = ErrNum
        m_By = By
        Dim bThrow As Boolean = False
        Select Case (m_ErrNum)
            Case BikiFrameResponse.CMD_BOOT
                m_ErrDesc = "Firmware Booting up OK"
            Case BikiFrameResponse.CMD_OK
                m_ErrDesc = "Command OK"
            Case BikiFrameResponse.CMD_FAILED
                m_ErrDesc = "Command Failed"
                bThrow = True
            Case BikiFrameResponse.CMD_CRC_FAILED
                m_ErrDesc = "CRC Failed"
                bThrow = True
            Case BikiFrameResponse.CMD_NEXT
                m_ErrDesc = "Command Next"
            Case BikiFrameResponse.CMD_BAD_HEADS
                m_ErrDesc = "Command Failed, Malformed. Update firmware"
                bThrow = True
            Case BikiFrameResponse.CMD_INCOMPLETE
                m_ErrDesc = "Command Failed, Incomplete. Update firmware"
                bThrow = True
            Case BikiFrameResponse.CMD_TIMEOUT
                m_ErrDesc = "Command Timed out"
                bThrow = True
            Case BikiFrameResponse.CMD_UNKNOWN
                m_ErrDesc = "Command Unknown, Update firmware"
                bThrow = True
            Case BikiFrameResponse.CMD_UNKNOWN_RECIVER
                m_ErrDesc = "Command Unknown Reciver, Update firmware"
                bThrow = True
            Case BikiFrameResponse.CMD_UNKNOWN_SENDER
                m_ErrDesc = "Command Unknown Sender, Update firmware"
                bThrow = True
            Case BikiFrameResponse.FLASH_WRITE_TIMEOUT
                m_ErrDesc = "Flash Write Timeout, try again"
                bThrow = True
            Case BikiFrameResponse.SEC_BAD_CODE
                m_ErrDesc = "Bad Security Code, try again"
                bThrow = True
            Case BikiFrameResponse.SEC_FUNCTION_LOCKED
                m_ErrDesc = "Function Locked, unlock via password and try again"
                bThrow = True
            Case BikiFrameResponse.SYNC_FAILED
                m_ErrDesc = "Communication Out of sync, try again"
                bThrow = True
            Case BikiFrameResponse.BOOT_FAILED
                m_ErrDesc = "Firmware Lock, Contact BadBiki Err #309"
                bThrow = True
            Case BikiFrameResponse.CMD_PASSED
                m_ErrDesc = "Diagnostics test Passed"
            Case BikiFrameResponse.CMD_NOTPASSED
                m_ErrDesc = "Diagnoistcs Test Failed!"
                bThrow = True
            Case BikiFrameResponse.CMD_TOOBIG
                m_ErrDesc = "Command size recieved too large"
                bThrow = True
            Case Else
                m_ErrDesc = "Unknown Error code, Update Bikisoft"
                bThrow = True
        End Select

        If bThrow Then
            Debug.Print("Comm_Exception: " & System.Enum.GetName(GetType(BikiFrameID), By) & " - " & m_ErrDesc)
            Throw Me
        End If
    End Sub

    Public Property Description() As String
        Get
            Return m_ErrDesc
        End Get
        Set(ByVal value As String)
            m_ErrDesc = value
        End Set
    End Property

    Public Property Response() As BikiFrameResponse
        Get
            Return m_ErrNum
        End Get
        Set(ByVal value As BikiFrameResponse)
            m_ErrNum = value
        End Set
    End Property

    Public Property By() As BikiFrameType
        Get
            Return m_By
        End Get
        Set(ByVal value As BikiFrameType)
            m_By = value
        End Set
    End Property
End Class

''' <summary>
''' USB Exceptions. Need to be cought. Only FT_OK doesn't throw and exception
''' </summary>
''' <remarks></remarks>
<Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> _
Public Class USB_exceptions
    Inherits System.Exception
    Private m_ErrDesc As String = ""
    Private m_ErrId As FT_return = FT_return.FT_OK
    Sub New(ByVal Description As String)
        m_ErrDesc = Description

        Debug.Print("USB Exception : " & Description)
        Throw Me
    End Sub
    Sub New(ByVal Id As FT_return)
        m_ErrId = Id
        Select Case Id
            Case FT_return.FT_OK
                m_ErrDesc = "Communication OK"
            Case FT_return.FT_IO_TIMEOUT
                m_ErrDesc = "Communication - Timeout"
            Case FT_return.FT_IO_FAILED
                m_ErrDesc = "Communication - IO Failed"
            Case FT_return.FT_OTHER_ERROR
                m_ErrDesc = "Communication Error, Comms blocked"
        End Select

        Debug.Print("USB Exception : " & m_ErrDesc)

        Select Case Id
            Case FT_return.FT_IO_TIMEOUT, FT_return.FT_IO_FAILED
                Throw Me
        End Select
    End Sub
    Public Property Description() As String
        Get
            Return m_ErrDesc
        End Get
        Set(ByVal value As String)
            m_ErrDesc = value
        End Set
    End Property
    Public ReadOnly Property Value() As FT_return
        Get
            Return m_ErrId
        End Get
    End Property
End Class
